<?php
//登入注册API控制器
namespace app\api\controller;

//TP请求模块
use think\facade\Request;
//TP验证模块
use think\exception\ValidateException;
//TP验证模块
use think\facade\Validate;
//TPCache模块
use think\facade\Cache;

//user模型
use app\api\model\User as UserModel;
//user验证器
use app\api\validate\User as UserValidate;
//公共类
use app\api\common\Common;


class PassPort
{
    //登入
    public function login()
    {
        //整理传参
        $email = Request::param('email');
        $password = Request::param('password');

        //验证参数是否合法
        try {
            validate(UserValidate::class)->batch(true)
                                         ->scene('login')
                                         ->check([
                'email' => $email,
                'password' => $password
            ]);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            $uservalidateerror = $e->getError();
            return Common::create($uservalidateerror,'登入失败',401);
        }

        //获取模型请求对象
        $result = UserModel::where('email', $email)
                            ->where('password', sha1($password))
                            ->find();

        //验证账号是否否存在
        if (empty($result)) {
            return Common::create([], '用户名或密码错误', 401);
        } else {
            //整理数据
            $uuid = Common::get_uuid();
            $uid = $result['uid'];
            $data = [
                'UUID' => $uuid
            ];
            //写入数据到缓存
            Cache::set( $uuid, $uid, 3600);
            //返回数据
            return Common::create($data, '登录成功', 200);
        }

    }

    //登出
    public function quit()
    {
        //整理传参
        if(empty(Request::param('UUID'))){
            return Common::create([], '请先登入', 500);
        }

        $uuid = Request::param('UUID');
        //判断参数是否存在
        if(!Cache::get($uuid)){
            return Common::create([], '退出失败', 500);
        }; 
        //删除缓存
        Cache::delete($uuid);
        return Common::create([], '退出成功', 200);

    }

    //注册
    public function register()
    {
        //整理传参
        $username = Request::param('username');
        $password = Request::param('password');
        $rpassword = Request::param('rpassword');
        $email = Request::param('email');

        //验证参数是否合法
        try {
            validate(UserValidate::class)->batch(true)
                                         ->scene('register')
                                         ->check([
                'username' => $username,
                'password' => $password,
                'rpassword' => $rpassword,
                'email' => $email
            ]);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            $uservalidateerror = $e->getError();
            return Common::create($uservalidateerror, '注册失败', 400);
        }

        //获取UID
        function get_uid()
        {

            //生成唯一值
            $result = substr(call_user_func('str_shuffle', time()), 0, -1);
            if (UserModel::where('uid', $result)) {
                return $result;
            } else {
                get_uid();
            }
        }

        //静态方法写入数据
        $user = UserModel::create([
            'uid'  =>  get_uid(),
            'email' =>  $email,
            'username'  =>  $username,
            'password'  =>  sha1($password),
            'login_date'  =>  '2022-01-01 00:00:00',
            'registe_date'  =>  date('Y-m-d h:i:s', time())
        ]);

        //整理数据
        $uuid = Common::get_uuid();
        $uid = $user->uid;
        $data = [
            'UUID' => $uuid
        ];
        //写入数据到缓存
        Cache::set( $uuid, $uid, 3600);

        return Common::create($data,'注册成功', 200);
    }
}
